class EventEmitter {
  constructor() {
    this.events = {}
  }

  // 注册事件监听器
  on(eventName, callback) {
    if (!this.events[eventName]) {
      this.events[eventName] = []
    }
    this.events[eventName].push(callback)

    // (this.events[eventName] ??= []).push(callback)
  }
  // 触发事件
  emit(eventName, ...args) {
    if (this.events[eventName]) {
      this.events[eventName].forEach(callback => {
        callback(...args)
      })
    }
  }
  // 移除事件监听器
  off(eventName, callback) {
    if (this.events[eventName]) {
      this.events[eventName] = this.events[eventName].filter(cb => cb !== callback)
    }
  }
}

// 创建 EventEmitter 实例
const eventEmitter = new EventEmitter()

export default eventEmitter
